﻿using ArticleWeb.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ArticleWeb.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        private ApplicationDbContext db;
        private ApplicationSignInManager _signInManager;
        private ApplicationUserManager _userManager;

        public HomeController()
        {
            db = new ApplicationDbContext();
        }

        public HomeController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
        {
            UserManager = userManager;
            SignInManager = signInManager;
        }

        public ApplicationSignInManager SignInManager
        {
            get
            {
                return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
            }
            private set
            {
                _signInManager = value;
            }
        }

        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }
        private string AEAdminID = "C555CF6A-6640-4CED-BB63-3EC52417AF62";
        [AllowAnonymous]
        public ActionResult Index()
        {
            if (Request.IsAuthenticated)
            {
                //var items = _GetSubCategories(null);
                return View();
            }
            
            var admin = UserManager.FindByName("AEAdmin"); ;
            if (admin == null)
            {
                admin = new Models.ApplicationUser();
                admin.Id = AEAdminID;
                admin.PhoneNumber = null;
                admin.PhoneNumberConfirmed = false;
                admin.RealName = "AEAdmin";
                admin.UserName = "AEAdmin";
                admin.TwoFactorEnabled = false;
                admin.Email = "aeadmin@meb.com";
                admin.EmailConfirmed = false;
                admin.CreateTime = DateTime.Now;
                admin.LastSyncAt = DateTime.Now;
                var result = UserManager.Create<ApplicationUser, string>(admin, "~Qq123456");
            }
            return Redirect("/Account/Login?ReturnUrl=%2F");
        }
        private List<IDName> _GetSubCategories(int? catId)
        {
            return db.ArticleCategories.Where(x => x.ParentID == catId).Select(x => new IDName { ID = x.ID, Name = x.Name }).ToList();
        }
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
                this.db?.Dispose();
            base.Dispose(disposing);
        }
    }
}